<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.formOfEmployment"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" disabled style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input
              v-model="userInfo.departmentName"
              class="inputW"
              @focus="showTree = true"
            />
            <div v-if="showTree" class="tree-box">
              <el-tree
                :data="treeData"
                default-expand-all
                :props="{ label: 'name' }"
                @node-click="handleNodeClick"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <UploadImg ref="uploadImg" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  getUserDetailByIdAPI,
  saveUserDetailByIdAPI,
  getDepartmentsListAPI
} from '@/api'
import EmployeeEnum from '@/api/constant/employees'
import { transTree } from '@/utils'
import dayjs from 'dayjs'
export default {
  data() {
    return {
      userId: this.$route.query.id, // 员工的id
      EmployeeEnum, // 员工枚举数据
      userInfo: {}, // 用户的所有信息
      showTree: false, // 控制树状结构
      treeData: [] // 树状结构的数组
    }
  },
  created() {
    this.loadUserInfoById()
    this.getUserInfoDepartmentFn()
  },
  methods: {
    // 获取用户信息
    async loadUserInfoById() {
      const res = await getUserDetailByIdAPI(this.userId).catch((err) => err)
      if (!res.success) return this.$message.error(res.message)
      this.userInfo = res.data
      this.userInfo.timeOfEntry = new Date(this.userInfo.timeOfEntry)
    },

    // 获取树状结构的部门信息
    async getUserInfoDepartmentFn() {
      const res = await getDepartmentsListAPI()
      this.treeData = transTree(res.data.depts, '')
    },

    // 保存更新 点击事件
    async saveUser() {
      const res = await saveUserDetailByIdAPI({
        ...this.userInfo,
        timeOfEntry: dayjs(this.userInfo.timeOfEntry).format('YYYY/MM/DD'),
        staffPhoto: this.$refs.uploadImg.imageUrl
      }).catch((err) => err)
      if (res.success) this.$message.success(res.message)
    },

    // 点击树状结构触发的事件
    handleNodeClick(data) {
      if (data && data.children) return
      this.userInfo.departmentName = data.name
      this.showTree = false
    }
  }
}
</script>

<style scoped lang="scss">
.user-info {
  padding-top: 20px;
}
</style>
